package com.czr.dao.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czr.dao.entity.*;
import com.czr.dao.mapper.CzrBusiMapper;
import com.czr.dao.mapper.CzrColumnMapper;
import com.czr.dao.mapper.CzrCompanyMapper;
import com.czr.dao.service.CzrBusiService;
import com.czr.dao.service.CzrCompanyService;
import com.czr.dao.service.CzrUserService;
import com.czr.dto.CompanyParam;
import com.czr.dto.CzrBusiParam;
import com.czr.dto.Search;
import com.czr.dto.StatusParam;
import com.czr.util.DateUtils;
import com.czr.util.PageUtil;
import com.czr.util.TencentCosUtil;
import com.czr.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 名企推荐 服务实现类
 * </p>
 */
@Service
public class CzrCompanyServiceImpl extends ServiceImpl<CzrCompanyMapper, CzrCompany> implements CzrCompanyService {

    @Autowired
    public CzrCompanyMapper czrCompanyMapper;

    @Override
    public Result<Object> list(CompanyParam companyParam) throws Exception {
        TencentCosUtil tencentCosUtil = new TencentCosUtil();
        Page<CzrCompany> page = new Page<>();
        page.setSize(companyParam.getSize());
        page.setCurrent(companyParam.getCurrent());
        LambdaQueryWrapper<CzrCompany> ew = new LambdaQueryWrapper<>();
        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(companyParam.getKeyword())) {
            ew.like(CzrCompany::getCompanyName, companyParam.getKeyword()).or().like(CzrCompany::getCompanyBusiness, companyParam.getKeyword());

        }
        if (!StringUtils.isEmpty(companyParam.getIsRecommend())) {
            ew.eq(CzrCompany::getIsCecommend, companyParam.getIsRecommend());
        }
        ew.eq(CzrCompany::getStatus, 0);
        ew.orderByAsc(CzrCompany::getCompanySort);
        page = this.getBaseMapper().selectPage(page, ew);
        JSONArray jsonArray = new JSONArray();
        for (CzrCompany czrCompany : page.getRecords()) {
            CzrCompanyVo czrCompanyVo = new CzrCompanyVo();
            BeanUtils.copyProperties(czrCompany, czrCompanyVo);
            if (!StringUtils.isEmpty(czrCompanyVo.getH5Logo())) {
                czrCompanyVo.setH5Logo(tencentCosUtil.imgFile_wx(czrCompanyVo.getH5Logo()));
            }
            if (!StringUtils.isEmpty(czrCompanyVo.getPcLogo())) {
                czrCompanyVo.setPcLogo(tencentCosUtil.imgFile_wx(czrCompanyVo.getPcLogo()));
            }
            jsonArray.add(czrCompanyVo);
        }
        JSONObject object = PageUtil.getPage(jsonArray, page);
        return Result.data(object);
    }

    @Override
    public Result<Map<String, Object>> add(CzrCompany czrCompany, Integer userId) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isEmpty(czrCompany.getId())) {
            //验证企业是否存在
            Map<String, Object> map = czrCompanyMapper.getCompanyName(czrCompany.getCompanyName());
            if (!StringUtils.isEmpty(map)) {
                return Result.success(201, "该企业已入库，请勿重复申请");
            }
        }
        if (StringUtils.isEmpty(czrCompany.getId())) {
            czrCompany.setUserId(userId);
            czrCompany.setStatus(0);
//            czrCompany.setIsCecommend(0);
            czrCompany.setInsertTime(format.format(new Date()));
        }
        czrCompany.insertOrUpdate();
        return Result.success(200, "成功！");
    }

    @Override
    public Result<Object> glCompanyList(CompanyParam companyParam) throws Exception {
        TencentCosUtil tencentCosUtil = new TencentCosUtil();
        Page<CzrCompany> page = new Page<>();
        page.setSize(companyParam.getSize());
        page.setCurrent(companyParam.getCurrent());
        LambdaQueryWrapper<CzrCompany> ew = new LambdaQueryWrapper<>();
        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(companyParam.getKeyword())) {
            ew.like(CzrCompany::getCompanyName, companyParam.getKeyword());

        }
        if (!StringUtils.isEmpty(companyParam.getIsRecommend())) {
            ew.eq(CzrCompany::getIsCecommend, companyParam.getIsRecommend());
        }
        ew.eq(CzrCompany::getStatus, 0);
        ew.orderByAsc(CzrCompany::getCompanySort);
        page = this.getBaseMapper().selectPage(page, ew);
        JSONArray jsonArray = new JSONArray();
        for (CzrCompany czrCompany : page.getRecords()) {
            if (!StringUtils.isEmpty(czrCompany.getH5Logo())) {
                czrCompany.setH5Logo(tencentCosUtil.imgFile_wx(czrCompany.getH5Logo()));
            }
            if (!StringUtils.isEmpty(czrCompany.getPcLogo())) {
                czrCompany.setPcLogo(tencentCosUtil.imgFile_wx(czrCompany.getPcLogo()));
            }
            jsonArray.add(czrCompany);
        }
        JSONObject object = PageUtil.getPage(jsonArray, page);
        return Result.data(page);
    }

    @Override
    public Result<Map<String, Object>> delCompany(Integer companyId, JSONObject cacheToken) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (!StringUtils.isEmpty(cacheToken) && cacheToken.getInteger("user_type") != 1) {
            return Result.success(201, "非管理员账号，无法删除新闻！");
        }
        CzrCompany czrCompany = czrCompanyMapper.selectById(companyId);
        if (StringUtils.isEmpty(czrCompany)) {
            return Result.success(201, "无效ID！");
        }
        czrCompany.setStatus(-1);
        boolean resultBoolean = czrCompany.updateById();
        if (resultBoolean) {
            return Result.success(200, "删除成功");
        } else {
            return Result.success(201, "删除失败！");
        }
    }

}
